﻿using System;

namespace Kiiro.Common.Utility
{
    /// <summary>
    /// <see cref="http://msdn.microsoft.com/en-us/library/5ata5aya.aspx"/>
    /// </summary>
    public static class DateTimeHelpers
    {
        public static DateTime Empty;

        #region Date Helper

        /// <summary>
        /// Is date today?
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsToday ( DateTime date )
        {
            return date.Date.CompareTo ( DateTime.Today.Date ) == 0;
        }

        /// <summary>
        /// Is date greater than Today?
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsDateEarlierThanToday ( DateTime date )
        {
            return DateTime.Today.Date.CompareTo ( date.Date ) < 0;
        }

        /// <summary>
        /// Is date less than Today?
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsDateLaterThanToday ( DateTime date )
        {
            return DateTime.Today.Date.CompareTo ( date.Date ) > 0;
        }

        /// <summary>
        /// Is date yesterday?
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsYesterday ( DateTime date )
        {
            return date.Date.CompareTo ( DateTime.Today.Date.AddDays ( -1 ) ) >= 0;
        }

        /// <summary>
        /// Is date within one week range?
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool WithinSevenDays ( DateTime date )
        {
            return date.Date.CompareTo ( DateTime.Today.Date.AddDays ( -7 ) ) >= 0;
        }

        /// <summary>
        /// Gets the number of work days between the start and end dates
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public static int GetNumberOfWorkDays ( DateTime startDate, DateTime endDate )
        {
            // clear any time data that may skew our comparison
            startDate = new DateTime ( startDate.Year, startDate.Month, startDate.Day );
            endDate = new DateTime ( endDate.Year, endDate.Month, endDate.Day );

            int numberOfWorkDays = 0;

            while ( startDate.CompareTo ( endDate ) < 1 )
            {
                if ( startDate.DayOfWeek != DayOfWeek.Saturday && startDate.DayOfWeek != DayOfWeek.Sunday )
                    numberOfWorkDays++;

                startDate = startDate.AddDays ( 1 );
            }

            return numberOfWorkDays;
        }

        public static DateTime AddWorkDays(DateTime date, int value)
        {
            int remaining;
            
            if (value > 5)
            {
                remaining = value % 5;
                int weeks = value / 5;
                date = date.AddDays(weeks * 7);
            }
            else
            {
                remaining = value;
            }

            while (remaining > 0)
            {
                date = date.AddDays(1);
    
                if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
                    remaining--;
            }

            return date;
        }

        #endregion
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Common/Utility/DateTimeHelpers.cs $
 * 
 * 4     30/09/09 9:11a Shafia
 * 
 * 3     17/08/09 10:38a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/